#include "LoginTask.h"

LoginTask::LoginTask(char* body, int client_fd, int len, int shmid, int msgid) :BaseTask(body, client_fd, len, shmid, msgid)
{

}

void LoginTask::working()
{	
	cout << "------------------------登录中------------------" << endl;
	LOGIN_REQ log_req = { 0 };
	//拷贝数据
	memcpy(&log_req, body, sizeof(LOGIN_REQ));

	//获取当前日期时间
	auto now = std::chrono::system_clock::now();
	auto in_time_t = std::chrono::system_clock::to_time_t(now);
	std::stringstream ss;
	ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %H:%M:%S");

	cout<< "时间: " << ss.str() << "\n"  // ctime自带换行
		<< "功能: 登录\n"
		<< "类型: 接收\n"
		<< "用户: " << log_req.usercount << "\n"
		<< "接收数据包:\n"
		<< "  密码: " << log_req.password << "\n"
		<< "  验证码: " << log_req.code << "\n"
		<< "--------------------------------------------" << "\n\n";
	cout << "实际验证码" << Codemap[client_fd] << endl;
	//写入日志文件
	LogManager& log_fd = LogManager::getInstance();
	ofstream& fd =  log_fd.getLogStream();
	fd<< "时间: " << ss.str()<<"\n"  // ctime自带换行
		<< "功能: 登录\n"
		<< "类型: 接收\n"
		<< "用户: " << log_req.usercount << "\n"
		<< "接收数据包:\n"
		<< "  密码: " << log_req.password << "\n"
		<< "  验证码: " << log_req.code << "\n"
		<< "--------------------------------------------" << "\n\n";

	// 4. 强制刷新缓冲区（确保日志立即写入文件）
	fd.flush();

	//解析
	string acount(log_req.usercount);
	string passwd (log_req.password);
	string code(log_req.code);
	shared_ptr<User> user =  model->getUser(acount, acount);
	if (!user)
	{
		send(Failure, "登录：用户不存在", User{}); // 发送用户不存在的错误
	}
	else if (strcmp(user->userpass, passwd.c_str()) == 0)
	{
		if (strcmp(code.c_str(), Codemap[client_fd].c_str()) == 0)
		{
			send(Success, "登录：登录成功",*user);
		}
		else
		{
			send(Failure, "登录：验证码有误",*user);
		}
	}
	else
	{
		send(Failure, "登录：用户名或密码有误",*user);
	}
}

void LoginTask::send(int statu,string message, User user)
{
	ShmControl* shm = (ShmControl*)shmat(shmid, NULL, 0);
	int index = -1;//空闲下标
	// 寻找下一个空闲块（状态0）
	for (int i = 0; i < MEMORYNUMBER; i++) {
		int status = shm->blocks[i].status;
		if (status == 0) { // 空闲
			index = i;
			break;
		}
	}
	LOGIN_RESP log_resp = { 0 };
	HEAD head = { 0 };
	char send[MEMORYSIZE] = { 0 };
	log_resp.status_code = statu;
	strcpy(log_resp.message, message.c_str());
	log_resp.user = user;
	//写头
	head.service_type = LOGIN;
	head.checksum = 100;
	head.data_len = sizeof(LOGIN_RESP);
	//头体结合
	memcpy(send, &head, sizeof(HEAD));
	memcpy(send + sizeof(HEAD), &log_resp, sizeof(LOGIN_RESP));
	//写入共享内存
	ShmBlock* block = &shm->blocks[index];
	memcpy(block->data, send, sizeof(block->data));//考入共享内存
	block->status = 1;                   // 标记为已写
	shmdt(shm);
	//写入消息队列
	MSG msg = { 0 };
	msg.client_fd = client_fd;
	msg.index = index;
	msg.mtype = 1;
	msgsnd(msgid, &msg, sizeof(MSG), 0);
	cout << "------------------------操作日志------------------" << endl;
	// 获取当前时间点（系统时钟）
	auto now = std::chrono::system_clock::now();
	auto in_time_t = std::chrono::system_clock::to_time_t(now);
	std::stringstream ss;
	ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %H:%M:%S");

	cout << "时间: " << ss.str();  // ctime自带换行
	cout << "功能：登录" << endl;
	cout << "类型：发送" << endl;
	cout << "用户id：" << user.id<< endl;
	cout << "发送数据包：" << endl;
	cout << "状态:" << statu<< "\n"
		<< "信息:" << message << endl;

	//写入文件
	LogManager& log_fd = LogManager::getInstance();
	ofstream& fd = log_fd.getLogStream();
	fd << "时间: " << ss.str() <<"\n"// ctime自带换行
		<< "功能：登录\n" 
		<< "类型：发送\n" 
		<< "用户id：\n" << user.id << "\n"
		<< "发送数据包：\n" 
		<< "状态:" << statu << "\n"
		<< "信息:" << message <<"\n"
		<< "--------------------------------------------" << "\n\n";
	// 4. 强制刷新缓冲区（确保日志立即写入文件）
	fd.flush();
}
